<?php
session_start();
class content extends myphp{
	function __construct(){
		$this->data=[
			$this->user=isset($_SESSION['user'])?$_SESSION['user']:null,
			'answer'=>$this->answer=isset($_POST['answer'])?$_POST['answer']:null,
			'loads'=>$this->gid=isset($_GET['id'])?$_GET['id']:null,
			'load'=>$this->id=isset($_POST['id'])?$_POST['id']:null,
			$this->title=isset($_POST['title'])?$_POST['title']:null,
			$this->pid=isset($_POST['pid'])?$_POST['pid']:null,
			'response'=>$this->response=isset($_POST['response'])?$_POST['response']:null,
			$this->ans=isset($_POST['ans'])?$_POST['ans']:null
		];
	}
	function article(){
		foreach($this->data as $k=>$v){
			if(!is_int($k) and isset($v)){
				$this->$k();
			}
		}
	}
	function loads(){
		$this->table('response')->select('count(id)',"pid=$this->gid ");
		foreach($this->res as $count);
		if($count[0]>=5 and file_exists("app/index/$this->gid.html")){
			header("location:app/index/$this->gid.html");
			exit;
		}
		$this->assigns();		
	}
	function load(){
		$this->table('search')->select('*',"id=$this->id");
		foreach($this->res as $article);
		$this->table('response')->select('cid,user,response',"pid=$this->id limit 5");
		$response=null;
		foreach($this->res as $comment){
				$response.='<br>'.'<a class="cid">'.$comment['cid'].'</a>'.'楼'.':'.'<a class="user">'.$comment['user'].'</a>'.':'.'&nbsp'.'<a class="response">'.$comment['response'].'</a>'.'<br>';
			}
		//$html=['id','title','datetime','source','content','response'];
		$art=['title'=>$article['title'],'datetime'=>$article['datetime'],'source'=>$article['source'],'cont'=>$article['content'],'resp'=>$response];
		echo json_encode($art);		
	}
	function response(){
		if(!isset($_SESSION['user'])){
			echo "请先登录!";
		}else{
	
			$this->table('response')->select('max(cid)',"pid='$this->id'");
			$responses=null;
			foreach($this->res as $comment);
			$cid=$comment[0]+1;
			$this->table('response')->insert('pid,cid,user,title,response',"'$this->id','$cid','$this->user','$this->title','$this->response'");
			$responses.='<br>'.'<a class="cid">'.$cid.'</a>'.'楼'.':'.'<a class="user">'.$this->user.'</a>'.':'.'&nbsp'.'<a class="response">'.$this->response.'</a>'.'<br>';
			echo $responses;
		}
	}
	function answer(){
		if(!isset($_SESSION['user'])){
			echo "请先登录!";
		}else{
	
			$this->table('answer')->insert('user,title,pid,answer,ans',"'$this->user','$this->title','$this->pid','$this->answer','$this->ans'");
			echo '<a class="user">'.$this->user.'</a>'.'回复'.'<a>'.$this->answer.'</a>'.':'.'<a class="responses">'.$this->ans.'</a>'.'<br>';	
		}
	}
}








	
